using System;
using System.Windows.Forms;
using Mbs.Elements;


namespace Mbs.Windows.Elements
{
	
	public class ToolStripItemCollectionElement : Element<ToolStripItem>
	{
		private ToolStripItemCollection collection;

		public ToolStripItemCollectionElement(ToolStripItemCollection collection)
		{
            if (collection == null)
                throw new ArgumentNullException("collection");
			this.collection = collection;
		}

		
		protected override ToolStripItem DoAdd(ToolStripItem uiElement)
		{
			if (collection == null)
				throw new InvalidOperationException();

            if (!collection.Contains(uiElement))
			    collection.Insert(GetInsertingIndex(uiElement), uiElement);
			return uiElement;
		}

		protected override void DoRemove(ToolStripItem uiElement)
		{
			if (uiElement.Owner != null && collection.Contains(uiElement))
				uiElement.Owner.Items.Remove(uiElement);
		}

		
		protected virtual int GetInsertingIndex(object uiElement)
		{
			return collection.Count;
		}

		protected ToolStripItemCollection InternalCollection
		{
			get { return collection; }
			set { collection = value; }
		}

        public override string Name { get; set; }

        public override object Tag { get; set; }

        public override string Text { get; set; }

        public override bool? Enabled { get; set; }
        public override bool? Visible { get; set; }
	}
}
